;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Barcode.BasisCode", [])
        .controller("BasisCodeCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.datePickerFlag = [false, false];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.markInfo = {};
            $scope.toolbarConfig = {
                showPrintConf: false,
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.tempSet = {
                qrCodeSize: 85,
                printMainCode: true,
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                fixedWidth: 140,
                scrollWidth: 1150,
                tableCol: [
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "入库单号",
                        dataField: "mainNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库存数量",
                        dataField: "totalQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "最小包装数量",
                        dataField: "minPackQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "图号",
                        dataField: "orawyd",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "VIN号",
                        dataField: "vinid",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "导入人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "导入时间",
                        dataField: "createTimeShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 100,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tbConf.fixedWidth + "px"}};
            };
            var lodop;

            var btnPermissionCode = {
                basisCodePrint: window.ActionCode.basisCodePrint
            };

            $scope.btnPermission = {
                basisCodePrint: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/initialInventory/listForPrint";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    $scope.markInfo.allChecked = false;
                    angular.forEach($scope.dataList, function (item) {
                        item.isChecked = false;
                        item.packNum = item.currentQty;
                        item.createTimeShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                    })
                });
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDatePicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };

            $scope.print = function (item, type) {
                var ids = [];
                ids.push(item.id);
                getBarCode(ids, type);
            };

            $scope.batchPrint = function (type) {
                var ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (item.isChecked) {
                        ids.push(item.id);
                    }
                });

                getBarCode(ids, type);
            };

            function getBarCode(ids, type) {
                var service = "device";
                var reqUrl = "/initialInventory/batchPrint";
                var param = ids;
                if (ids.length === 0) {
                    ybWidget.ShowMessage('请选择要打印的零件');
                    return;
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    startPrint(type, resp.value, true);
                });
            }

            $scope.printList = [];
            async function startPrint(type, printListArr, isBatch) {
                if (!$scope.tempSet.printMainCode) {
                    for (var i = 0; i < printListArr.length; i++) {
                        if (printListArr[i].barcodeFlag === 0) {
                            printListArr.splice(i, 1);

                            if (!isBatch) { // 非批量打印，只有一个总条码，找到 即 退出for循环
                                break;
                            }
                        }
                    }
                }
                
                $scope.printList = type === "preview" && printType !== 1 ? printListArr : UserContextService.ListGroup(printListArr, 20);
                
                var printType = $scope.tempSet.printType;
                lodop = UserContextService.GetLodop();
                if (!lodop) {
                    return;
                }

                if (!await UserContextService.InitPrinter(lodop, true, printType)) {
                    return;
                }

                setTimeout(function () {
                    UserContextService.ListPrint($scope.printList, lodop, printType, type === "preview");

                    $scope.printList = [];
                    getDataList();
                }, 300)
            }

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);